﻿using UnityEngine;
using GameFramework;
using GameFramework.Fsm;
using ProcedureOwner = GameFramework.Fsm.IFsm<TowerDefence_Zombie.EntityEnemy>;

namespace TowerDefence_Zombie
{
    public class EnemyDeadState : FsmState<EntityEnemy>, IReference
    {
        private EntityEnemy owner;
        protected EntityTargetable m_TargetTower;
        private float Delaytimer;

        public EnemyDeadState() { }

        protected override void OnUpdate(ProcedureOwner fsm, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(fsm, elapseSeconds, realElapseSeconds);
            if (Delaytimer > 0)
            {
                Delaytimer -= elapseSeconds;
                if (Delaytimer <= 0)
                    owner.DeadEvent();
            }
            else if (owner.delayTiem == 0)
                owner.DeadEvent();
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            owner = procedureOwner.Owner;
            owner.Agent.isStopped = true;
            Delaytimer = owner.delayTiem;
            if (owner.animator != null)
                owner.animator.SetTrigger("Death");
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            owner = null;
            m_TargetTower = null;
        }

        public static EnemyDeadState Create()
        {
            EnemyDeadState state = ReferencePool.Acquire<EnemyDeadState>();
            return state;
        }

        public void Clear()
        {
            owner = null;
            m_TargetTower = null;
        }
    }
}
